package Day03;

import java.util.ArrayList;
import java.util.List;

/**
 * @author SunYuHang
 * @date 2023-01-05 15:38
 * @ClassName : 括号生成  //类名
 */

public class 括号生成 {
    List<String> res = new ArrayList<>();

    public List<String> generateParenthesis(int n) {
        StringBuilder sb = new StringBuilder();
        solve(0,n*2,sb);
        return res;
    }
    void solve(int leftCnt,int n,StringBuilder sb){
        if (sb.length() == n){
            res.add(sb.toString());
            return;
        }

        if (leftCnt>0){
            sb.append(')');
            solve(leftCnt-1,n,sb);
            sb.deleteCharAt(sb.length()-1);
        }

        if (n-sb.length()>leftCnt){
            sb.append('(');
            solve(leftCnt+1,n,sb);
            sb.deleteCharAt(sb.length()-1);
        }

    }
}
